// Filename: views/projects/list
define([
  'jquery',
  'jqueryupload',
  'underscore',
  'backbone',
  // Pull in the Collection module from above,
  'collections/response/ExistingTemplatesCollection',
  'text!templates/response/manageResponseListTemplate.html',
  'text!templates/response/editResponseTemplate.html'
], function($, JQueryUpload, _,Backbone, ExistingTemplatesCollection, manageResponseListTemplate,editResponseTemplate){
  var ManageResponseListView = Backbone.View.extend({
    el: $("#templates"),
    events: {
    	/*'click #editFileUpload' : 'editUpload'*/
    	} ,

    render: function(){
      var data = {
        templates: this.collection.models,
        _: _ 
      };

      var compiledTemplate = _.template( manageResponseListTemplate, data );
      $("#templates").html( compiledTemplate );
      $('.filename').click(function(event) {
    	  var topicId =$(this).attr("topicId");
    	  window.open("getTemplate/"+topicId,"_blank");
      });
      $(".templatetable tr .editdialog").click(function(){
    	    var topic = $(this).closest('tr').find('td:first').text();
    	    var id = $(this).closest('tr').find('td:first').attr("topicid");
    	  //  var fileName = $(this).find('td:eq(1)').text();
    	    var data = {
    	            topic: topic,
    	            id:id,
    	            _: _ 
    	          };
    	    var compiledDialogTemplate = _.template( editResponseTemplate, data );
    	    $(compiledDialogTemplate).dialog({height:300,width:1000,
    	    	buttons: [
    	    	          {
    	    	            text: "Save",
    	    	            click: function() {
    	    	            	var self=$(this);
    	    	            	if($("#editProblemDesc").val() == ""||$('#newFileName').val()==""){
    	    	            		 $.msgBox({
    	    	         			    title:"Appment",
    	    	         			    content:"Problem Description and file are mandatory!",
    	    	         			    type:"error"
    	    	         			});
    	    	            	}else{
        	    	            	var otherdata ={};
        	    	            	otherdata["topic"]=$("#editProblemDesc").val();
        	    	            	otherdata["id"]=$("#editProblemDesc").attr("topicid");
        	    	  	  	        $.ajax("postUserTemplate", {
        	    	    	  	            data: otherdata,
        	    	    	  	            dataType: "json",
        	    	    	  	          type: "POST",
        	    	    	  	            files: $("#editForm").find(":file"),
        	    	    	  	            iframe: true,
        	    	    	  	            processData: false
        	    	    	  	          }).always(function() {
        	    	    	  	        	  
        	    	    	  	        	 $.msgBox({
     	     	    	         			    title:"Appment",
     	     	    	         			    content:"Saved Successfully!",
     	     	    	         			    type:"info",
     	     	    	  				    success: function (result) {
     	     	    	  				    	self.dialog("close");
	     	     	    	  				   	  var existingTemplatesCollection = new ExistingTemplatesCollection();
		     	     	 	  			    	   var lOptions = {};
		     	     	 	  			           lOptions.success = function(){
		     	     	 	  			           	var manageResponseListView = new ManageResponseListView({ collection: existingTemplatesCollection});
		     	     	 	  			           	manageResponseListView.render();
		     	     	 	  			           	//self.manageResponseListView =manageResponseListView; 
		     	     	 	  			           };
		     	     	 	  			           existingTemplatesCollection.fetch(lOptions);

     	     	    	  				    }
     	     	    	         			});
        	    	    	  	          }).done(function(data) {
        	    	    	            		

        	    	    	  	          });
    	    	            		
    	    	            	}

    	    	            }
    	    	          }
    	    	        ]});
    	    $('.file-wrapper1 input[type=file]')
  	  	  .bind('change', function(){
  	  		  var $this = $(this),
  		        $val = $this.val(),
  		        valArray = $val.split('\\');
  		        newVal = valArray[valArray.length-1];
  		  	  $('#newFileName').val(newVal);
  	  	  });
    	});
    },
    editUpload: function(){
    	alert();
        $.ajax(form.prop("action"), {
          //  data: form.find("textarea").serializeArray(),
            dataType: "json",
            files: form.find(":file"),
            iframe: true,
            processData: false
          }).always(function() {}).done(function(data) {});
    }
  });
  return ManageResponseListView;
});
